package com.example.pongv1;
import org.andengine.engine.Engine;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.ui.IGameInterface.OnCreateSceneCallback;

import com.example.pongv1.BaseScene;
public class SceneManager {
	
	//declaration de scenes.
	 private BaseScene splashScene;
	    private BaseScene menuScene;
	    private BaseScene gameScene;
	    private BaseScene loadingScene;
	    private BaseScene optionScene;
	    private BaseScene infoScene;
	  //---------------------------------------------
	    // VARIABLES
	    //---------------------------------------------
	    
	    private static final SceneManager INSTANCE = new SceneManager();
	    
	    private SceneType currentSceneType = SceneType.SCENE_SPLASH;
	    
	    private BaseScene currentScene;
	    
	    private Engine engine = RessourcesManager.getInstance().engine;
	    
	    public enum SceneType
	    {
	        SCENE_SPLASH,
	        SCENE_MENU,
	        SCENE_GAME,
	        SCENE_LOADING,
	        SCENE_OPTION,
	        SCENE_INFO
	    }
	    
	    //---------------------------------------------
	    // CLASS LOGIC
	    //---------------------------------------------
	    
	    public void setScene(BaseScene scene)
	    {
	        engine.setScene(scene);
	        currentScene = scene;
	        currentSceneType = scene.getSceneType();
	    }
	    
	    public void setScene(SceneType sceneType)
	    {
	        switch (sceneType)
	        {
	            case SCENE_MENU:
	                setScene(menuScene);
	                break;
	            case SCENE_GAME:
	                setScene(gameScene);
	                break;
	            case SCENE_SPLASH:
	                setScene(splashScene);
	                break;
	            case SCENE_LOADING:
	                setScene(loadingScene);
	                break;
	            case SCENE_OPTION:
	                setScene(optionScene);
	                break;
	            case SCENE_INFO:
	                setScene(infoScene);
	                break;
	                
	            default:
	                break;
	        }
	    }
	    
	    //---------------------------------------------
	    // GETTERS AND SETTERS
	    //---------------------------------------------
	    
	    public static SceneManager getInstance()
	    {
	        return INSTANCE;
	    }
	    
	    public SceneType getCurrentSceneType()
	    {
	        return currentSceneType;
	    }
	    
	    public BaseScene getCurrentScene()
	    {
	        return currentScene;
	    }
	    
	    public void createSplashScene(OnCreateSceneCallback pOnCreateSceneCallback)
	    {
	        RessourcesManager.getInstance().loadSplashScreen();
	        splashScene = new SplashScene();
	        currentScene = splashScene;
	        pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
	    }
	    
	    private void disposeSplashScene()
	    {
	        RessourcesManager.getInstance().unloadSplashScreen();
	        splashScene.disposeScene();
	        splashScene = null;
	    }
	   
	    public void createMenuScene()
	    {
	        RessourcesManager.getInstance().loadMenuResources();
	        menuScene = new MyMenuScene();
	        loadingScene = new LoadingScene();
	        
	        setScene(menuScene);
	        disposeSplashScene();
	        
	    }
	    
	    public void loadGameScene(final Engine mEngine)
	    {
	        setScene(loadingScene);
	        RessourcesManager.getInstance().unloadMenuTextures();
	        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
	        {
	            public void onTimePassed(final TimerHandler pTimerHandler) 
	            {
	                mEngine.unregisterUpdateHandler(pTimerHandler);
	                RessourcesManager.getInstance().loadGameResources();
	                gameScene = new PongScene();
	                RessourcesManager.getInstance().music.pause();
	                setScene(gameScene);
	            }
	        }));
	    }
	    
	    public void loadOptionScene()
	    {
	    	
	    	RessourcesManager.getInstance().loadOptionResources();
	    	RessourcesManager.getInstance().unloadMenuTextures();
	    	optionScene= new OptionScene();
	       menuScene.disposeScene();
	        setScene(optionScene);
	      
	    }
	    public void loadInfoScene()
	    {
	    	
	    	RessourcesManager.getInstance().loadInfoResources();
	    	RessourcesManager.getInstance().unloadMenuTextures();
	    	infoScene= new InfoScene();
	       menuScene.disposeScene();
	        setScene(infoScene);
	      
	    }
	    public void loadMenuScene()
	    {
	    	RessourcesManager.getInstance().loadMenuTextures();
	    	if(gameScene!=null)
	    	{
	    	gameScene.disposeScene();
	    	}
	    	setScene(menuScene);
	    	
	    }
	   

}
